package cetcbigdata.da.zhengwugongkai;

import cetcbigdata.da.base.DownloadAndUploadMinio;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


public class util {
    //参数格式化输出
    public static String ParameterFormat(int taskType){
        String TaskType = String.format("%02d", taskType);
        if (TaskType.equals("11")){
            TaskType = "20";
        }
        return TaskType;
    }
    //获取特殊程序种类
    public static String[] getNEType( String noExtendType) {
        String [] nett;
        nett = noExtendType.split(",");
        for (int i = 0; i < nett.length; i++) {

            switch (nett[i]) {
                case "01":
                    nett[i] = "书面审查";
                    break;
                case "02":
                    nett[i] = "实地核查";
                    break;
                case "03":
                    nett[i] = "招标与拍卖";
                    break;
                case "04":
                    nett[i] = "检验";
                    break;
                case "05":
                    nett[i] = "检测";
                    break;
                case "06":
                    nett[i] = "检疫";
                    break;
                case "07":
                    nett[i] = "鉴定";
                    break;
                case "08":
                    nett[i] = "考试";
                    break;
                case "09":
                    nett[i] = "考核";
                    break;
                case "10":
                    nett[i] = "专家评审";
                    break;
                case "11":
                    nett[i] = "技术审查";
                    break;
                case "12":
                    nett[i] = "听证";
                    break;
                case "13":
                    nett[i] = "听取利害关系人意见";
                    break;
                case "14":
                    nett[i] = "集体审查";
                    break;
                case "15":
                    nett[i] = "法律法规或规章规定的其他审查方式";
                    break;
            }
        }
        return nett;

    }
    public static String IsSpecial(String html) {
        if(html.equals("0"))
        {
            return "否";
        }
        return "是";
    }

    //获取法人事项主题
    public static String getSxzt1(String sxzt) {


        switch (sxzt) {
            case "005":
                sxzt = "设立变更";
                break;
            case "010":
                sxzt = "准营准办";
                break;
            case "015":
                sxzt= "资质认证";
                break;
            case "020":
                sxzt= "年检年审";
                break;
            case "025":
                sxzt= "税收财务";
                break;
            case "030":
                sxzt = "人力资源";
                break;
            case "035":
                sxzt = "社会保障";
                break;
            case "040":
                sxzt = "投资审批";
                break;
            case "045":
                sxzt= "融资信贷";
                break;
            case "050":
                sxzt = "抵押质押";
                break;
            case "055":
                sxzt = "商务贸易";
                break;
            case "060":
                sxzt = "招标拍卖";
                break;
            case "065":
                sxzt = "海关口岸";
                break;
            case "070":
                sxzt = "涉外服务";
                break;
            case "075":
                sxzt = "农林牧渔";
                break;
            case "085":
                sxzt = "国土和规划建设";
                break;
            case "090":
                sxzt = "交通运输";
                break;
            case "095":
                sxzt = "环保绿化";
                break;
            case "100":
                sxzt = "应对气候变化";
                break;
            case "105":
                sxzt = "水务气象";
                break;
            case "110":
                sxzt = "医疗卫生";
                break;
            case "115":
                sxzt = "科技创新";
                break;
            case "120":
                sxzt = "文体教育";
                break;
            case "125":
                sxzt = "知识产权";
                break;
            case "130":
                sxzt = "民族宗教";
                break;
            case "135":
                sxzt = "质量技术";
                break;
            case "140":
                sxzt = "检验检疫";
                break;
            case "145":
                sxzt = "安全生产";
                break;
            case "150":
                sxzt = "公安消防";
                break;
            case "155":
                sxzt = "司法公证";
                break;
            case "160":
                sxzt = "公用事业";
                break;
            case "165":
                sxzt = "法人注销";
                break;
            case "170":
                sxzt = "档案文物";
                break;
            case "998":
                sxzt= "地方特色分类";
                break;
            case "999":
                sxzt= "其他";
                break;
        }

        return sxzt;
    }
    //获取自然人事项主题
    public static String getSxzt2(String sxzt) {

        switch (sxzt) {
            case "005":
                sxzt = "生育收养";
                break;
            case "010":
                sxzt = "户籍办理";
                break;
            case "015":
                sxzt = "民族宗教";
                break;
            case "020":
                sxzt = "教育科研";
                break;
            case "025":
                sxzt = "入伍服役";
                break;
            case "030":
                sxzt= "就业创业";
                break;
            case "035":
                sxzt= "设立变更";
                break;
            case "040":
                sxzt = "准营准办";
                break;
            case "045":
                sxzt = "抵押质押";
                break;
            case "050":
                sxzt = "职业资格";
                break;
            case "055":
                sxzt = "行政缴费";
                break;
            case "060":
                sxzt = "婚姻登记";
                break;
            case "065":
                sxzt = "优待抚恤";
                break;
            case "070":
                sxzt = "规划建设";
                break;
            case "075":
                sxzt = "住房保障";
                break;
            case "085":
                sxzt = "社会保障（社会保险、社会救助）";
                break;
            case "090":
                sxzt = "证件办理";
                break;
            case "095":
                sxzt = "交通出行";
                break;
            case "100":
                sxzt = "旅游观光";
                break;
            case "105":
                sxzt = "出境入境";
                break;
            case "110":
                sxzt = "消费维权";
                break;
            case "115":
                sxzt = "公共安全";
                break;
            case "120":
                sxzt = "司法公证";
                break;
            case "125":
                sxzt = "知识产权";
                break;
            case "130":
                sxzt = "环保绿化";
                break;
            case "135":
                sxzt= "文化体育";
                break;
            case "140":
                sxzt = "公用事业";
                break;
            case "145":
                sxzt = "医疗卫生";
                break;
            case "150":
                sxzt = "离职退休";
                break;
            case "155":
                sxzt = "死亡殡葬";
                break;

        }

        return sxzt;
    }
    //    获取服务对象
    public static String[] getfwdx(String fwdx) {
//        "serviceCode\":\"1,2,3,4,9,5,6\
        String[] sites;
        sites = fwdx.split(",");
        for (int i = 0; i < sites.length; i++) {
            switch (sites[i]) {
                case "1":
                    sites[i] = "自然人";
                    break;
                case "2":
                    sites[i] = "企业法人";
                    break;
                case "3":
                    sites[i] = "事业法人";
                    break;
                case "4":
                    sites[i] = "社会组织法人";
                    break;
                case "5":
                    sites[i] = "非法人企业";
                    break;
                case "6":
                    sites[i] = "行政机关";
                    break;
                case "9":
                    sites[i] = "其他组织";
                    break;
            }
        }
        return sites;
    }

    //获取事项类型
    public static String getTaskType(String tasktp) {
        switch (tasktp) {
            case "01":
                tasktp = "行政许可";
                break;
            case "02":
                tasktp = "行政处罚";
                break;
            case "03":
                tasktp = "行政强制";
                break;
            case "04":
                tasktp = "行政征收";
                break;
            case "05":
                tasktp = "行政给付";
                break;
            case "06":
                tasktp = "行政检查";
                break;
            case "07":
                tasktp = "行政确认";
                break;
            case "08":
                tasktp = "行政奖励";
                break;
            case "09":
                tasktp = "行政裁决";
                break;
            case "10":
                tasktp = "其他行政权力";
                break;
            case "20":
                tasktp = "公共服务";
                break;
        }
        return tasktp;
    }
    public static String ProjectType (String json) {
        if(StringUtils.isBlank(json)){
            json="无";
        }
        else if(json.equals("1"))
        {
            json="即办件";
        }
        else {
            json="承诺件";
        }
        return json;
    }
    //网办深度
    public static String[] getWbsd(String wbsd) {

        String[] fwdxArr = wbsd.split(",");
        for (int i = 0; i < fwdxArr.length; i++) {
            switch (fwdxArr[i]) {
                case "1":
                    fwdxArr[i] = "互联网咨询";
                    break;
                case "2":
                    fwdxArr[i] = "互联网收件";
                    break;
                case "3":
                    fwdxArr[i] = "互联网预审";
                    break;
                case "4":
                    fwdxArr[i] = "互联网受理";
                    break;
                case "5":
                    fwdxArr[i] = "互联网办理";
                    break;
                case "6":
                    fwdxArr[i] = "互联网办理结果信息反馈";
                    break;
                case "7":
                    fwdxArr[i] = "互联网电子证照反馈";
                    break;
                case "9":
                    fwdxArr[i] = "其他";
                    break;
                default:
                    fwdxArr[i] = "无";
                    break;
            }
        }
        return fwdxArr;
    }

    public static String[] getxscj(String xscj){
        String[] xscjArr = xscj.split(",");
        for (int i = 0; i < xscjArr.length; i++) {
            switch (xscjArr[i]) {
                case "1":
                    xscjArr[i] = "国家级";
                    break;
                case "2":
                    xscjArr[i] = "省级";
                    break;
                case "3":
                    xscjArr[i] = "市级";
                    break;
                case "4":
                    xscjArr[i] = "县区级";
                    break;
                case "5":
                    xscjArr[i] = "乡镇级";
                    break;
                case "6":
                    xscjArr[i] = "村级";
                    break;
            }
        }
        return xscjArr;
    }
    //权力来源
    public static  String getPowerSource(String qlly) {
        switch (qlly) {
            case "1":
                qlly = "法定本级行使";
                break;
            case "2":
                qlly= "上级下放";
                break;
            case "3":
                qlly = "上级授权";
                break;
            case "4":
                qlly = "同级授权";
                break;
            case "5":
                qlly = "上级委托";
                break;
            case "6":
                qlly = "同级委托";
                break;

        }
        return qlly;
    }
    //审批结果
    public static String getType(String spjg) {
        switch (spjg) {
            case "1":
                spjg = "证明";
                break;
            case "2":
                spjg = "证照";
                break;
            case "4":
                spjg = "批文";
                break;
            case "9":
                spjg = "其他";
                break;
            case "99":
                spjg = "无";
                break;
            default:
                spjg = "无";
                break;
        }
        return spjg;
    }

    //通办范围
    public static String getHandleArea(String ha) {
        switch (ha) {
            case "1":
                ha = "全国";
                break;
            case "2":
                ha = "全省";
                break;
            case "3":
                ha = "全市";
                break;
            case "4":
                ha = "全县";
                break;
            case "5":
                ha = "全镇(乡、街道)";
                break;
            case "6":
                ha = "跨村(社区)";
                break;
        }
        return ha;
    }

    //获取办理形式
    public static String[] getBLstyle(String blstr) {
        String [] blArr = blstr.split(",");
        for (int i = 0; i < blArr.length; i++) {
            switch (blArr[i]) {
                case "1":
                    blArr[i] = "窗口办理";
                    break;
                case "2":
                    blArr[i] = "网上办理";
                    break;
                case "3":
                    blArr[i] = "快递申请";
                    break;
            }
        }
        return blArr;
    }

    //获取移动端办理类型
    public static String[] getAppType(String AppType) {
        String[] AppTypeArr = AppType.split(",");
        for (int i = 0; i < AppTypeArr.length; i++) {
            switch (AppTypeArr[i]) {
                case "1":
                    AppTypeArr[i] = "移动APP";
                    break;
                case "2":
                    AppTypeArr[i] = "支付宝小程序";
                    break;
                case "3":
                    AppTypeArr[i] = "微信小程序";
                    break;
            }
        }
        return AppTypeArr;

    }

    //获取移动端服务内容
    public static String[] getAppDepth(String AppDepth) {
        String [] AppDepthArr = AppDepth.split(",");
        for (int i = 0; i < AppDepthArr.length; i++) {
            switch (AppDepthArr[i]) {
                case "1":
                    AppDepthArr[i] = "预约";
                    break;
                case "2":
                    AppDepthArr[i] = "查询";
                    break;
                case "3":
                    AppDepthArr[i] = "办理";
                    break;
                case "4":
                    AppDepthArr[i] = "评价";
                    break;
            }
        }
        return AppDepthArr;
    }
    //获取材料分类
    public static String getClfl(String clfl) {
        switch (clfl) {
            case "0":
                clfl = "申请表";
                break;
            case "1":
                clfl = "证明";
                break;
            case "2":
                clfl = "证照";
                break;
            case "3":
                clfl = "报告";
                break;
            case "4":
                clfl = "批文";
                break;
            case "9":
                clfl = "其他";
                break;
        }
        return clfl;
    }
    //获取来源渠道
    public static String getSourceType(String type) {
        switch (type) {
            case "10":
                type = "申请人自备";
                break;
            case "20":
                type = "政府部门核发";
                break;
            case "99":
                type = "其他";
                break;
        }
        return type;
    }

    public static String fileDownloadAndUploadMinioGet(String url, String fileName) {
        try {
            JSONObject param = new JSONObject();
            JSONObject headers = new JSONObject();
            return DownloadAndUploadMinio.getInstance().downloadGet(
                    url,
                    headers,
                    param,
                    fileName
            );
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    public static  String downLoadFile(String strUrl, String fileName){
        // String strUrl = "http://zwfw.hubei.gov.cn//web/lcfile/group1/M00/00/4D/wKhUBF1VBAyAZ3OXAANoFLMh7QU722.jpg";
        try {
            //构造URL
            URL url = new URL(strUrl);
            //构造连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //这个网站要模拟浏览器才行
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
            //打开连接
            conn.connect();
            //打开这个网站的输入流
            InputStream inStream = conn.getInputStream();
            //用这个做中转站 ，把图片数据都放在了这里，再调用toByteArray()即可获得数据的byte数组
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            //用这个是很好的，不用一次就把图片读到了文件中
            //要是需要把图片用作其他用途呢？所以直接把图片的数据弄成一个变量，十分有用
            //相当于操作这个变量就能操作图片了
            byte[] buf = new byte[1024];
            //为什么是1024？
            //1024Byte=1KB，分配1KB的缓存
            //这个就是循环读取，是一个临时空间，多大都没关系
            // 这没有什么大的关系，你就是用999这样的数字也没有问题，就是每次读取的最大字节数。
            //byte[]的大小，说明你一次操作最大字节是多少
            //虽然读的是9M的文件，其实你的内存只用1M来处理，节省了很多空间．
            //当然，设得小，说明I/O操作会比较频繁，I/O操作耗时比较长，
            // 这多少会有点性能上的影响．这看你是想用空间换时间，还是想用时间换空间了．
            //时间慢总比内存溢出程序崩溃强．如果内存足够的话，我会考虑设大点．
            int len = 0;
            //读取图片数据
            while ((len = inStream.read(buf)) != -1) {
                outStream.write(buf, 0, len);
            }
            inStream.close();
            outStream.close();
            //把图片数据填入文件中
            File file = new File(fileName);
            FileOutputStream op = new FileOutputStream(file);
            op.write(outStream.toByteArray());
            op.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }

    public static  String downLoadFileUpLoadMinio(String strUrl, String fileName){
        // String strUrl = "http://zwfw.hubei.gov.cn//web/lcfile/group1/M00/00/4D/wKhUBF1VBAyAZ3OXAANoFLMh7QU722.jpg";
        try {
            //构造URL
            URL url = new URL(strUrl);
            //构造连接
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            //这个网站要模拟浏览器才行
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko");
            //打开连接
            conn.connect();
            //打开这个网站的输入流
            InputStream inStream = conn.getInputStream();
            String id = DownloadAndUploadMinio.getInstance().uploadFileToMinio(inStream, "chengdu", fileName);
            return id;
        }catch (Exception e){
            e.printStackTrace();
        }
        return "";
    }
}
